/*
 * Copyright 2006-2007 Queplix Corp.
 *
 * Licensed under the Modified Queplix BSD License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.queplix.com/BSDLICENSE-1.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.jlect.swebing.ui.client.button;

import com.jlect.swebing.renderers.client.Image;
import com.jlect.swebing.renderers.client.RendererActionListener;
import com.jlect.swebing.renderers.client.RendererAdapter;
import com.jlect.swebing.renderers.client.ToggleButtonRenderer;
import com.jlect.swebing.ui.client.Color;
import com.jlect.swebing.ui.client.GLabel;
import com.jlect.swebing.ui.client.MouseEvent;
import com.jlect.swebing.ui.client.MouseListener;
import com.jlect.swebing.ui.client.laf.UIManager;

/**
 * Toggle button renderer adapter, delegates general calls' methods to the
 * container.
 *
 * @author Sergey Kozmin
 * @since Nov 23, 2007
 */
public class ToggleButtonRendererAdapter extends RendererAdapter implements ToggleButtonRenderer,
        MouseListener {
    private GLabel label;
    private RendererActionListener listener;

    private boolean pressedView = false;

    /**
     * Creates ToggleButtonRendererAdapter instance, that will delegate all events to the upper level renderer
     * @param label label
     */
    public ToggleButtonRendererAdapter(GLabel label) {
        this.label = label;
    }

    public void setText(String text) {
        label.setText(text);
    }

    public void setIcon(Image image) {
        //@todo implement me
    }

    public void setPressed(boolean pressed) {
        setPressedView(pressed);
    }

    public void setSelected(boolean selected) {
//        empty
    }

    public void setActionListener(RendererActionListener listener) {
        this.listener = listener;
        if(listener != null) {
            label.addMouseListener(this);
        } else {
            label.removeMouseListener(this);
        }
    }

    public void mouseClicked(MouseEvent e) {
        pressedView = false;
        if(listener != null) {
            listener.fireActionPerformed(e.getInitial());
        }
    }

    public void mousePressed(MouseEvent e) {
        pressedView = true;
        setPressedView(true);
    }

    public void mouseReleased(MouseEvent e) {
//        do nothing mouse click event will change view
    }

    public void mouseEntered(MouseEvent e) {
        if(pressedView) {
            setPressedView(true);
        }
    }

    public void mouseExited(MouseEvent e) {
        if(pressedView) {
            setPressedView(false);
        }
    }

    private void setPressedView(boolean viewPressed) {
        label.setBackground(viewPressed ?
                getBackgroundSelected():getBackground());
        label.setForeground(viewPressed ?
                getForegroundSelected():getForeground());
    }

    private Color getBackground() {
        return UIManager.getInstance().getColor("Menu.background");
    }

    private Color getForeground() {
        return UIManager.getInstance().getColor("Menu.foreground");
    }

    private Color getForegroundSelected() {
        return UIManager.getInstance().getColor("Menu.selectionForeground");
    }

    private Color getBackgroundSelected() {
        return UIManager.getInstance().getColor("Menu.selectionBackground");
    }
}
